﻿Imports System.Threading
Public Class FormUI
    'declare our delegates
    Delegate Sub UpdateSystemLogDelegate(ByVal statusString As String)
    Delegate Sub UpdateTotalTweetsFound(ByVal TotalTweets As String)
    Delegate Sub UpdateTotalImagesFound(ByVal TotalImages As String)
    Delegate Sub UpdateLatestTweetID(ByVal latestTweetID As String)
    Delegate Sub updateImagesDownloaded(ByVal imagesDownloaded As String)
    Delegate Sub updateTimeTillNextQuery(ByVal timeLeft As Integer)

    Public Sub updateTimeLeft(ByVal timeleft As Integer)
        If InvokeRequired Then
            Dim updateTimeTillNextQueryDel As updateTimeTillNextQuery = New updateTimeTillNextQuery(AddressOf updateTimeLeft)
            Me.Invoke(updateTimeTillNextQueryDel, timeleft)
        Else
            If timeleft = 7 Then
                Me.pbTimeTillNextQuery.Value = 0
            Else
                Me.pbTimeTillNextQuery.PerformStep()
            End If

        End If
    End Sub

    Public Sub UpdateSystemLog(ByVal statusString As String)
        'check to see if the update needs to be invoked by the UI thread or not
        If InvokeRequired Then
            'build delegate object and address it to its own method
            'method gets called a second time  already mashalled to UI thread and will no longer need to invoke so it will go straight to the 
            'meat of this method
            Dim updateSystemLogBoxDel As UpdateSystemLogDelegate = New UpdateSystemLogDelegate(AddressOf UpdateSystemLog)
            Me.Invoke(updateSystemLogBoxDel, statusString)
        Else
            'meat of this method
            Me.txtSystemLog.Text = statusString
        End If
    End Sub

    Public Sub UpdateTotalTweets(ByVal totalTweets As String)
        If InvokeRequired Then
            Dim updateTotalTweetsDel As UpdateTotalTweetsFound = New UpdateTotalTweetsFound(AddressOf UpdateTotalTweets)
            Me.Invoke(updateTotalTweetsDel, totalTweets)
        Else
            Me.txtTotalTweets.Text = totalTweets
        End If
    End Sub

    Public Sub UpdateTotalImages(ByVal totalImages As String)
        If InvokeRequired Then
            Dim updateTotalImagesDel As UpdateTotalImagesFound = New UpdateTotalImagesFound(AddressOf UpdateTotalImages)
            Me.Invoke(updateTotalImagesDel, totalImages)
        Else
            Me.txtTotalImages.Text = totalImages
        End If
    End Sub

    Public Sub UpdateLatestTweet(ByVal latestTweetID As String)
        If InvokeRequired Then
            Dim updateLatestTweetDel As UpdateLatestTweetID = New UpdateLatestTweetID(AddressOf UpdateLatestTweet)
            Me.Invoke(updateLatestTweetDel, latestTweetID)
        Else
            Me.txtLatestTweetID.Text = latestTweetID
        End If
    End Sub

    Public Sub updateTotalImagesDownloaded(ByVal imagesDownloaded As String)
        If InvokeRequired Then
            Dim updateImagesDownloadedDel As updateImagesDownloaded = New updateImagesDownloaded(AddressOf updateTotalImagesDownloaded)
            Me.Invoke(updateImagesDownloadedDel, imagesDownloaded)
        Else
            Me.txtImagesDownloaded.Text = imagesDownloaded
        End If
    End Sub

    Private Sub btnConnect_Click(sender As System.Object, e As System.EventArgs) Handles btnConnect.Click
        Me.btnConnect.Enabled = False
        Me.txtTotalTweets.Text = 0
        Me.txtTotalImages.Text = 0
        Me.txtLatestTweetID.Text = ""
        BGW_connectToTwitter.RunWorkerAsync()
        Me.btnConnect.Enabled = True
    End Sub

    Private Sub BGW_connectToTwitter_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BGW_connectToTwitter.DoWork
        Dim twitterSearch As New Twitter()
        Dim image As New Image()
        Dim response As Boolean
        Dim searchString As String
        Dim imageList As List(Of String)
        Dim i = 0
        Dim j = 0
        response = twitterSearch.getRequestResponse
        image.getNumberOfImages()
        Do While i < 170
            If response = True Then
                searchString = HttpUtility.UrlEncode(Me.txtSearchString.Text)
                If Len(Me.txtLatestTweetID.Text) > 0 Then
                    searchString += "&since_id=" & Me.txtLatestTweetID.Text
                End If
                imageList = twitterSearch.search(searchString)
                image.downloadimages(imageList)
                image.getNumberOfImages()
            End If
            'sleep for 6 seconds so we don't hit our query limit
            j = 0
            Do While j < 7
                Thread.Sleep(1000)
                j = j + 1
                Me.updateTimeLeft(j)
            Loop
            i = i + 1
        Loop

    End Sub
End Class
